<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table>
        <tr>
            <td>标题</td>
            <td><input type="text" name="headLine"></td>
        </tr>
        <tr>
            <td>摘要</td>
            <td><input type="text" name="abstract"></td>
        </tr>
        <tr>
            <td>内容</td>
            <td><input type="text" name="content"></td>
        </tr>
        <tr>
            <td>分类</td>
            <td><input type="text" name="classify"></td>
        </tr>
        <tr>
            <td>作者</td>
            <td><input type="text" name="author"></td>
        </tr>
        <tr>
            <td>发表时间</td>
            <td><input type="text" name="postedTimeg"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消并返回" onclick="cencel()"></td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <script src="./js/api.js"></script>
    <script>
         $(function () {
          let id = getUrlParam('id');
          id=id || 0;
          getBlogById(id).then(res=>{
              let obj = res.data;
              $('[name=headLine]').val(obj.headLine)
              $('[name=abstract]').val(obj.abstract)
              $('[name=content]').val(obj.content)
              $('[name=classify]').val(obj.classify) 
              $('[name=author]').val(obj.author) 
              $('[name=postedTimeg]').val(obj.postedTimeg) 
          })  
        })

        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r  = window.location.search.substr(1).match(reg);
            if(r!=null) return unescape(r[2]); return null;
        }

        function save() {
            let id = getUrlParam('id');
            let obj={
                headLine:$('[name=headLine]').val(),
                abstract:$('[name=abstract]').val(),
                content:$('[name=content]').val(),
                classify:$('[name=classify]').val(),
                author:$('[name=author]').val(),
                postedTimeg:$('[name=postedTimeg]').val(),
            }
            if (id) {
                updateBlog(id, obj).then(res => {
                    console.log(res);
                    location.href = './index.html';
                })
            }else{
                let params = {
                    headLine:obj.headLine,
                    abstract:obj.abstract,
                    content:obj.content,
                    classify:obj.classify,
                    author:obj.author,
                    postedTimeg:obj.postedTimeg,

                }
                addBlog(params).then(res=>{
                    location.href='./index.html'
                })
            }
        }
        function cencel() {
            location.href='./index.html'
        }
    </script>
</body>
</html>